using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using Deepcode.Flux.Core.Systems.Workflow;
using Deepcode.Flux.Core.Systems.Workflow.Bases;

using Deepcode.Flux.Core.Services.State;
using Example.Workflow.Objects;

public partial class extensions_exampleworkflow_manage_ui_Form : Deepcode.Flux.Core.UI.AdministrationModulePage
{
	#region ViewState storage
	/// <summary>
	/// Stores the state of the listing view
	/// </summary>
	public string ListingState
	{
		get
		{
			return (string)ViewState["ListingState"];
		}
		set
		{
			ViewState["ListingState"] = value;
		}
	}
	/// <summary>
	/// Stores the ID of the record being edited
	/// </summary>
	public int RecordID
	{
		get
		{
			object o = ViewState["RecordID"];
			if (o == null) return -1;
			return (int)o;
		}
		set
		{
			ViewState["RecordID"] = value;
		}
	}
	#endregion
	#region Page Load Event
	/// <summary>
	/// Page Load event
	/// </summary>
	/// <param name="sender"></param>
	/// <param name="e"></param>
	protected void Page_Load(object sender, EventArgs e)
	{
		if (!Page.IsPostBack)
		{
			// Store any state information from the calling listing
			this.ListingState = GetAttribute("state", "");
			this.RecordID = GetAttribute("ID", -1);

			// Populate the workflow options
			Definition [] defs = new Definitions().GetDefinitionsByHandlerID("TelephoneBook");
			// Clear out the DDL
			ddlWorkflows.Items.Clear();
			foreach (Definition Def in defs)
				ddlWorkflows.Items.Add( new ListItem( Def.DefName, Def.PK_ID.ToString()));

			// Load the record if specified
			if (this.RecordID != -1)
			{
				TelephoneBook book = new TelephoneBook();
				TelephoneBookEntry entry = book.GetTelephoneBookByID( this.RecordID );
				if( entry == null ) GoBack();

				fName.Text = entry.Name;
				fTel.Text = entry.Tel;
				fNotes.Text = entry.Notes;
				fHistory.Text = entry.ApprovalHistory.Replace("\n", "<br/>");
				try { ddlWorkflows.SelectedValue = entry.FK_WorkflowDefinition.ToString(); }
				catch { }
			}
		}
	}
	#endregion
	#region Navigation
	/// <summary>
	/// Returns to the listing, restoring state
	/// </summary>
	protected void GoBack()
	{
		string url = String.Format("default.aspx?state={0}", this.ListingState);
		Response.Redirect(url, true);
	}
	#endregion
	#region Save/Cancel handlers
	/// <summary>
	/// User cancelled the save, go back to previous page
	/// </summary>
	/// <param name="sender"></param>
	/// <param name="e"></param>
	protected void btnCancel_Click(object sender, EventArgs e)
	{
		GoBack();
	}
	/// <summary>
	/// User clicked the save button, store the data
	/// </summary>
	/// <param name="sender"></param>
	/// <param name="e"></param>
	protected void btnSave_Click(object sender, EventArgs e)
	{
		if (!Page.IsValid) return;

		TelephoneBook book = new TelephoneBook();
		TelephoneBookEntry entry = null;

		if (this.RecordID != -1)
		{
			// Load the existing record and ensure it still exists
			entry = book.GetTelephoneBookByID(this.RecordID);
			if (entry == null) GoBack();
		}
		else
		{
			entry = new TelephoneBookEntry();
		}
		// Set the workflow info to unapproved and set which def is being used to process
		entry.State = 0;
		entry.FK_WorkflowDefinition = Int32.Parse(ddlWorkflows.SelectedValue);
		// Set the name and save the record
		entry.Name = fName.Text;
		entry.Tel = fTel.Text;
		entry.Notes = fNotes.Text;
		book.Save( entry );

		// Start the workflow
		WorkflowEngine engine = new WorkflowEngine("TelephoneBook");
		engine.ClearProcessingFor( entry.PK_ID );
		engine.BeginProcessing( entry.FK_WorkflowDefinition, entry.PK_ID, entry.Name );

		// Redirect back to main form
		GoBack();
	}
	#endregion
}
